#!/usr/env/bin python
# -*- coding:utf-8 -*-
import paramiko
import time


class HandleParamiko:
    """
    define one dowith class
    """

    def __init__(self, host, port, username, password):
        self.host = host
        self.port = port
        self.username = username
        self.password = password

    def get_interface_list(self):
        cmd = 'ifconfig | grep flags | cut -d ":" -f 1'
        return self.remote_exec_command(cmd)

    def get_interface_info(self, interface):
        ip_address_cmd = "ifconfig " + interface + " | grep 'inet ' | awk '{print $2}'"
        ret_ip = self.remote_exec_command(ip_address_cmd)
        mask_cmd = "ifconfig " + interface + " | grep 'netmask ' | awk '{print $4}'"
        ret_mask = self.remote_exec_command(mask_cmd)
        return str(ret_ip.get('stdout')).strip(), str(ret_mask.get('stdout')).strip()

    def remote_exec_command(self, command):
        try:
            # 创建SSH客户端
            ssh_client = paramiko.SSHClient()
            # 自动添加主机密钥
            # 使用 Paramiko 库进行 SSH 连接时，
            # 当连接到一个之前没有连接过的主机时，会出现一个警告，提示用户主机的密钥没有被认证。
            # 这时候可以使用 set_missing_host_key_policy() 方法来设置主机密钥的策略。
            # AutoAddPolicy() 会自动将新的主机密钥添加到本地的 known_hosts 文件中，
            # 以便下次连接时可以自动认证主机密钥，而不会再次出现警告。
            ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)

            # 连接远程服务器
            ssh_client.connect(self.host, self.port, self.username, self.password)

            # stdio 是输入， stdout 是输出，stderr错误输出
            stdio, stdout, stderr = ssh_client.exec_command(command)
            channel = stdout.channel
            status = channel.recv_exit_status()

            # stdout标准输出读取数据
            stdout = stdout.read().decode("utf-8")

            # stderr标准错误数据输出读取数据
            stderr = stderr.read().decode("utf-8")

            # 使用return 返回数据
            return {"stderr": stderr, "stdout": stdout, "status": status}

        except Exception as e:
            print(e)

        finally:
            try:
                ssh_client.close()
            except Exception as e:
                print(e)


if __name__ == "__main__":
    cmd = 'ifconfig'
    ip = '172.31.1.238'
    username = 'zk'
    password = 'zonkin123'
    port = 22
    # host, port, username, passwordd
    hp = HandleParamiko(ip, port, username, password)
    ret = hp.get_interface_list()
    interface_list = str(ret.get('stdout')).split("\n")[0:-1]
    print(type(interface_list), interface_list)
    dic = {}
    for interface in interface_list:
        ip, mask = hp.get_interface_info(interface)
        if ip == "" or mask == "":
            continue
        dic[interface] = [ip, mask]
    print(dic)

    # 输出结果中包含了每个网卡的配置信息，我们只关心inet地址部分

    # cmd = 'date'
    # ip = '172.31.1.237'
    # username = 'zk'
    # password = 'zonkin123'
    # port = 22
    # # host, port, username, passwordd
    # hp = HandleParamiko(ip, port, username, password)
    #
    # strcmd = 'echo "' + password + '" | sudo -S chmod 777 /home/zk/LST/project/ptp_time/zk_getdate_install.sh'
    # print(strcmd)
    # ret = hp.remote_exec_command(strcmd)
    #
    # print(ret.get('stdout'), ret.get('stderr'), ret.get('status'))
    # strcmd = 'echo "' + password+'" | sudo -S /home/zk/LST/project/ptp_time/zk_getdate_install.sh'
    # print(strcmd)
    # ret = hp.remote_exec_command(strcmd)
    # print(ret.get('stdout'), ret.get('stderr'), ret.get('status'))
